package cn.bytepulse.utils;

import cn.bytepulse.web.admin.entity.SysUser;
import cn.bytepulse.web.auth.dao.LoginUser;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Set;


/**
 * nginx工具类
 *
 * @author jiejiebiezheyang
 * @since 2023-05-20 21:31
 */
public class ReqUtils {

    /**
     * 配置了nginx反向代理后,获取ip
     */
    public static String getIP(HttpServletRequest request) {
        String ip = null;
        ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        //使用代理，则获取第一个IP地址
        if (StringUtils.isEmpty(ip) && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }

    /**
     * 获取ip
     */
    public static String getIP() {
        return getIP(getRequest());
    }

    /**
     * 获取当前请求对象
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        return requestAttributes.getRequest();
    }

    /**
     * 获取当前用户id
     */
    public static Integer getUserId() {
        if (ReqUtils.getRequest().getAttribute("userId") == null) {
            return 0; //这里应该是返回一个默认的用户id，所以这里返回0
        }
        return Integer.valueOf((String) ReqUtils.getRequest().getAttribute("userId"));
    }

    /**
     * 获取当前用户权限
     */
    public static Set<String> getPermissions() {
        return getLoginUser().getPermissions();
    }

    /**
     * 获取当前用户名称
     */
    public static String getUserName() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getName();
    }

    /**
     * 获取当前用户
     */
    public static SysUser getUser() {
        return getLoginUser().getSysUser();
    }

    /**
     * 获取当前认证用户
     */
    public static LoginUser getLoginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (LoginUser) authentication.getPrincipal();
    }

    /**
     * 路径是否匹配
     */
    public static boolean isPathMatching(List<String> paths, String apiPath) {
        AntPathMatcher matcher = new AntPathMatcher();
        for (String path : paths) {
            if (matcher.match(path, apiPath)) {
                return true;
            }
        }
        return false;
    }
}
